package com.my.zidingyikongjian.view.two;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.view.View;

/**
 * 项目名称：ZiDingYiKongJian
 * 创建人：
 * 创建时间：2018/7/10 16:14
 */

public class DrawText extends View {
    private Context context;

    private int flag;
    String bi = "哔哩哔哩 - ( ゜- ゜)つロ 乾杯~ - bilibili";

    public DrawText(Context context, int flag) {
        super(context);
        this.flag = flag;
        this.context = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(5); // 画笔宽度
        paint.setAntiAlias(true); // 抗锯齿
        paint.setTextSize(60); // 文字大小

        if (flag == 0) { // 文字三种样式

            // 绘制样式，设置为填充
            paint.setStyle(Paint.Style.FILL); // 填充
            canvas.drawText(bi, 10, 100, paint);

            // 绘制样式，设置为描边
            paint.setStyle(Paint.Style.STROKE); // 描边
            canvas.drawText(bi, 10, 200, paint);

            // 绘制样式，设置为填充
            paint.setStyle(Paint.Style.FILL_AND_STROKE); // 填充且描边
            canvas.drawText(bi, 10, 300, paint);

        } else if (flag == 1) {
            paint.setStyle(Paint.Style.FILL); // 填充
            // 设置样式
            paint.setFakeBoldText(true); // 粗体文字
            paint.setUnderlineText(true); // 下划线
            paint.setStrikeThruText(true);// 删除效果的线
            // 设置字体水平倾斜度，普通字体-0.25，可见向右倾斜
            paint.setTextSkewX((float) -0.25);
            canvas.drawText(bi, 10, 100, paint);

            // 设置字体水平倾斜度，普通字体0.25，可见向左倾斜
            paint.setTextSkewX((float) 0.25);
            canvas.drawText(bi, 10, 200, paint);
        } else if (flag == 2) {
            paint.setStyle(Paint.Style.FILL);
            // 普通样式
            canvas.drawText(bi, 10, 100, paint);
            // 水平方向拉伸两倍 高度不变
            paint.setTextScaleX(2);
            canvas.drawText(bi, 10, 200, paint);

            //写在同一位置,不同颜色,看下高度是否看的不变
            paint.setTextScaleX(1);
            canvas.drawText(bi, 10, 300, paint);

            //写在同一位置,不同颜色,看下高度是否看的不变
            paint.setColor(Color.RED);
            paint.setTextScaleX(2);
            canvas.drawText(bi, 10, 300, paint);
        } else if (flag == 3) { // 指定文字位置
            float[] pos = new float[]{80, 100, 80, 200, 80, 300, 80, 400};
            canvas.drawPosText("哔哩哔哩", pos, paint);
        } else if (flag == 4) { // 延路径绘制
            paint.setStyle(Paint.Style.STROKE);
            paint.setTextSize(45); // 设置文字大小
            paint.setColor(Color.GREEN);
            String string = "无奈王已随风去，世间再无幻想乡";
            // 创建两个相同的圆形路径,并先画两个路径原图
            Path circlePath = new Path();
            // 逆向绘制一个圆
            circlePath.addCircle(220, 200, 180, Path.Direction.CCW);
            canvas.drawPath(circlePath, paint); // 绘制出路径的原型
            Path circlePath2 = new Path();
            circlePath2.addCircle(750, 200, 180, Path.Direction.CCW);
            canvas.drawPath(circlePath2, paint); // 画出路径

            // hoffset,voffset参数值全部设置为0，看看原始状态
            canvas.drawTextOnPath(string, circlePath, 0, 0, paint);
            // 第二个路径，改变hOffset和vOffset参数值
            canvas.drawTextOnPath(string, circlePath2, 80, 30, paint);
        } else if (flag == 5) { // 加载自定义字体
            // 创建系统自带字体绘制
            paint.setColor(Color.RED);
            paint.setStrokeWidth(5); // 画笔宽度
            paint.setAntiAlias(true); // 抗锯齿
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(60); // 文字大小

            String familyName = "宋体";
            Typeface font = Typeface.create(familyName, Typeface.NORMAL);
            paint.setTypeface(font);
            canvas.drawText("无奈王已随风去，世间再无幻想乡", 10, 100, paint);

            AssetManager manager = context.getAssets(); // 得到AssetManager
            Typeface typeface = Typeface.createFromAsset(manager, "jian_luobo.ttf");
            paint.setTypeface(typeface);
            canvas.drawText("无奈王已随风去，世间再无幻想乡", 10, 200, paint);
        }
    }
}
